<?php

namespace App\Services;

use App\Exceptions\ValidationException;
use App\Repositories\Contracts\ActivityFormRepository;
use App\Repositories\Contracts\ActivityRepository;
use App\Repositories\Transformers\ActivityFormTransformer;

class ActivityFormService
{
    private $activityFormRepository;
    private $activityRepository;

    public function __construct(ActivityFormRepository $activityFormRepository, ActivityRepository $activityRepository)
    {
        $this->activityFormRepository = $activityFormRepository;
        $this->activityRepository = $activityRepository;
    }

    /**
     * 获取列表
     *
     * @param array|null $param
     */
    public function getList($param = null)
    {
        try {
            $filter['add_uid'] = $param['add_uid'];

            $activityForm = $this->activityFormRepository->paginate($param['per_page'] ?? null, $filter);
            $result = fractal($activityForm, new ActivityFormTransformer());

            return $result->toArray();

        } catch (\Throwable $th) {
            throw $th;
        }
    }

    /**
     * 获取详情
     *
     * @param int $id
     * @return object|array
     */
    public function getInfo($id)
    {
        try {
            $result = $this->activityFormRepository->find($id);
            return $result;
        } catch (\Throwable $th) {
            throw $th;
        }
    }

    /**
     * 新增
     *
     * @param array $param
     */
    public function add($param)
    {
        try {
            // 先判断是否已提交过
            $where = [
                'activity_id' => $param['activity_id'],
                'add_uid' => $param['add_uid'],
            ];
            $form = $this->activityFormRepository->findByFilter($where);

            if ($form) {
                throw new ValidationException('已提交过');
            }
            // 查询活动限制人数
            $activity = $this->activityRepository->findByFilter(['id' => $param['activity_id']], ['id', 'limit_count']);
            
            if (!$activity) {
                throw new ValidationException('活动未找到');
            }
            // 判断人数是否已满
            $activityNum = $this->activityFormRepository->findNumByActivityId($param['activity_id']);

            if ($activity->limit_count <= $activityNum) {
                throw new ValidationException('活动人数已满');
            }
            
            $activityForm = $this->activityFormRepository->store($param);
            return $activityForm;
        } catch (\Throwable $th) {
            throw $th;
        }
    }

    /**
     * 修改
     *
     * @param array $param
     * @param int $id
     */
    public function update($param, $id)
    {
        try {
            $activityForm = $this->activityFormRepository->update($param, $id);
            return $activityForm;
        } catch (\Throwable $th) {
            throw $th;
        }
    }

    /**
     * 删除
     *
     * @param int $id
     */
    public function delete($id)
    {
        try {
            $activityForm = $this->activityFormRepository->delete($id);

            $result = [];
            if ($activityForm) {
                $result = 'success';
            } else {
                $result = 'error';
            }
            return $result;

        } catch (\Throwable $th) {
            throw $th;
        }
    }
}
